#include "hplcsparklinkdefine.h"
#include "hplcsparklinkcheckpacket.h"
#include "hplcsparklinkpacketformat.h"
#include "basesendpacket.h"
#include "hplcsparklinkprotocolcommondata.h"
#include <QDebug>
#include <QCoreApplication>

namespace hplc
{
namespace sparklink
{

HplcSparkLinkCheckPacket::HplcSparkLinkCheckPacket(HplcSparkLinkProtocolCommonData &data, QObject *parent)
    : QObject (parent)
{
    m_d = &data;

    m_d->m_isWaitReply = false;
    m_d->m_waitPfc = HplcSparkLinkAutoSamplerPfcId_None;

    m_d->m_waitReplyTimerNumber = SPRAKLINK_PROTOCOL_WAIT_REPLY_TIMEOUT;
}

HplcSparkLinkCheckPacket::~HplcSparkLinkCheckPacket()
{

}

void HplcSparkLinkCheckPacket::setMaxWaitTime(quint32 ms)
{
    m_d->m_waitReplyTimerNumber = ms;
}

quint32 HplcSparkLinkCheckPacket::getMaxWaitTime()
{
    return m_d->m_waitReplyTimerNumber;
}

QByteArray HplcSparkLinkCheckPacket::getReply()
{
    return m_d->m_rxPacket.buffer;
}

Reply HplcSparkLinkCheckPacket::getReplyCode()
{
    return m_d->m_reply;
}

void HplcSparkLinkCheckPacket::replyCheck(char re)
{
    m_d->m_isWaitReply = false;

    switch (re)
    {
    case SPARKLINK_PROTOCOL_ACK:
    {
        m_d->m_reply = Reply_ack;
    }
        break;
    case SPARKLINK_PROTOCOL_NACK:
    {
        // 帧错误
        qDebug() << "nack";
        emit nack();
        m_d->m_reply = Reply_nack;
    }
        break;
    case SPARKLINK_PROTOCOL_NACK0:
    {
        // 帧错误
        qDebug() << "nack0";
        emit nack0();
        m_d->m_reply = Reply_nack0;
    }
        break;
    default:
        qDebug() << "error reply" << re;
        break;
    }
}

void HplcSparkLinkCheckPacket::replyCheck(HplcSparkLinkAutoSamplerPfcId pfc)
{
    if (m_d->m_waitPfc == pfc)
    {
        m_d->m_isWaitReply = false;
        m_d->m_reply = Reply_ok;
    }
}

void HplcSparkLinkCheckPacket::waitReply(HplcSparkLinkAutoSamplerPfcId pfc)
{
//    m_d->m_txBufferAsciiList << m_d->m_txBuffer;

    m_d->m_waitReplyTimerStart = true;
    m_d->m_waitReplyTimerCount = 0;

    m_d->m_waitPfc = pfc;
    m_d->m_isWaitReply = true;
    m_d->m_reply = Reply_waiting;

    qDebug() << "wait....." << pfc;

    while (m_d->m_isWaitReply) // 等待回复
    {
//        QApplication::exec(); // Qt事件循环
        QCoreApplication::processEvents(); // Qt事件循环
    }
    qDebug() << "quit...==============================" << m_d->m_waitPfc;

    m_d->m_waitPfc = HplcSparkLinkAutoSamplerPfcId_None;
    m_d->m_waitReplyTimerStart = false;

//    if (m_d->m_txBufferAsciiList.size())
//        m_d->m_txBufferAsciiList.removeFirst();
}

void HplcSparkLinkCheckPacket::send()
{
    emit m_d->m_sender->requestSend(m_d->m_txBuffer);
}

void HplcSparkLinkCheckPacket::onBaseTickTimeout()
{
    if (m_d->m_waitReplyTimerStart)
    {
        m_d->m_waitReplyTimerCount += HPLC_SPARKLINK_BASE_TICK_MS;
        if (m_d->m_waitReplyTimerCount >= m_d->m_waitReplyTimerNumber)
        {
            m_d->m_waitReplyTimerCount = 0;
            m_d->m_isWaitReply = false; // 退出等待回复

            emit noReply();
            m_d->m_reply = Reply_none;
        }
    }
}

} // sparklink
} // hplc
